
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return '';
}
function getCookie(c_name) {
    if(document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");//获取字符串的起点
        if(c_start != -1) {
            c_start = c_start + c_name.length + 1;//获取值的起点
            c_end = document.cookie.indexOf(";", c_start);//获取结尾处
            if(c_end == -1) c_end = document.cookie.length;//如果是最后一个，结尾就是cookie字符串的结尾
            return decodeURI(document.cookie.substring(c_start, c_end));//截取字符串返回
        }
    }
    return "";
}
// function setCookie(c_name, value, expiredays) {
//     var exdate = new Date();
//     exdate.setTime(Number(exdate) + expiredays);
//     document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
// }
// setCookie("___openid","ss");
var ___openid = getCookie('___openid');
var ___code = getQueryString('code');
if(___code != '' && ___openid == ''){
    $(function(){
        $.ajax({
            type : "get",
            url : "/api/answer/wx/getopenid",
            dataType : "json",
            async : false,
            data:{code:___code},
            success : function(data) {
                console.log("success.");
            },
            error: function(xhr, status, error) {
                console.log("获取客户信息失败.");
                window.onload=function(){
                    window.location.href="http://yd.sxycy.cn/views/answer/wx/wxerror.html";
                };
            }
        })
    });
}else if(___openid == ''){
    var url = location.href.split('#')[0].toString();
    window.onload=function(){
        window.location.href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxd1e48d57d90f603b&redirect_uri="+url+"&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
    };
}else{
    console.log(___openid);
}